package com.sgai.zero_robot.pop;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.sgai.zero_robot.R;
import com.sgai.zero_robot.core.RobotManagerFactory;
import com.sgai.zero_robot.core.callback.SpeedRatioCallback;


public class SettingSpeedRatioView extends FrameLayout implements View.OnClickListener {

    private EditText etSpeed,etMaxSpeed;
    private TextView btSet,btSetMax;

    public SettingSpeedRatioView(@NonNull Context context) {
        this(context, null);
    }

    public SettingSpeedRatioView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public SettingSpeedRatioView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private final String TAG = "SettingSpeedRatioView";
    private Handler handler = new Handler(Looper.getMainLooper());

    private void init(Context context) {
        View view = inflate(context, R.layout.setting_speed_ratio, this);
        etSpeed = view.findViewById(R.id.et_speed);
        etMaxSpeed = view.findViewById(R.id.et_max_speed);
        btSet = view.findViewById(R.id.bt_set);
        btSetMax = view.findViewById(R.id.bt_set_max);
        btSet.setOnClickListener(this);
        btSetMax.setOnClickListener(this);
    }

    private SpeedRatioCallback speedRatioCallback = new SpeedRatioCallback() {
        @Override
        public void onSpeedRatio(String speedRatio) {
            Log.e(TAG, "onSpeedRatio: " + speedRatio);
            setRatio(speedRatio);
        }
    };

    private void setRatio(String speedRatio) {
        if (etSpeed != null && !TextUtils.isEmpty(speedRatio)) {
            etSpeed.setText(speedRatio);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.e(TAG, "onAttachedToWindow: ");
        setRatio(RobotManagerFactory.getInstance().rateValue);
        RobotManagerFactory.getInstance().addSpeedRatioCallback(speedRatioCallback);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.e(TAG, "onDetachedFromWindow: ");
        RobotManagerFactory.getInstance().removeSpeedRatioCallback(speedRatioCallback);
    }

    @Override
    public void onClick(View v) {
        if (v == btSet) {
            String string = etSpeed.getText().toString();
            if (TextUtils.isEmpty(string)) {
                Toast.makeText(getContext(), "转速调整比例不能为空！", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(string);
            if (i<500||i>2000){
                Toast.makeText(getContext(), "转速调整比例范围在500-2000之间，请确认参数", Toast.LENGTH_SHORT).show();
                return;
            }
            RobotManagerFactory.getInstance().setRate(i);
        }else if (v == btSetMax){
            String string = etMaxSpeed.getText().toString();
            if (TextUtils.isEmpty(string)) {
                Toast.makeText(getContext(), "转速限速比例不能为空！", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(string);
            if (i<1||i>10){
                Toast.makeText(getContext(), "转速限速比例范围在1-10之间，请确认参数", Toast.LENGTH_SHORT).show();
                return;
            }
            RobotManagerFactory.getInstance().setMaxSpeed(i);
        }
    }
}
